package com.leo;

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.util.Callback;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName TableViewStudentProDemo
 * @Description 加载map
 * @Author wangss
 * @date 2020.06.13 20:35
 * @Version 1.0
 */
public class TableViewMapDemo extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        AnchorPane anchorPane = new AnchorPane();
        anchorPane.setStyle("-fx-background-color: #fff2f2");

        Map<String, SimpleStringProperty> map1 = new HashMap<>();
        map1.put("name", new SimpleStringProperty("小a"));
        map1.put("age", new SimpleStringProperty("12"));
        map1.put("flag", new SimpleStringProperty("true"));

        Map<String, SimpleStringProperty> map2 = new HashMap<>();
        map2.put("name", new SimpleStringProperty("小b"));
        map2.put("age", new SimpleStringProperty("10"));
        map2.put("flag", new SimpleStringProperty("false"));


        ObservableList<Map<String, SimpleStringProperty>> list = FXCollections.observableArrayList();
        list.addAll(map1);
        list.addAll(map2);

        TableView<Map<String, SimpleStringProperty>> tableView = new TableView<>(list);

        TableColumn<Map<String, SimpleStringProperty>, String> name = new TableColumn<>("姓名");
//        name.setCellValueFactory(new MapValueFactory("name"));
        name.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Map<String, SimpleStringProperty>, String>, ObservableValue<String>>() {
            @Override
            public ObservableValue<String> call(TableColumn.CellDataFeatures<Map<String, SimpleStringProperty>, String> param) {
                return param.getValue().get("name");
            }
        });

        TableColumn<Map<String, SimpleStringProperty>, String> age = new TableColumn<>("年龄");
        age.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Map<String, SimpleStringProperty>, String>, ObservableValue<String>>() {
            @Override
            public ObservableValue<String> call(TableColumn.CellDataFeatures<Map<String, SimpleStringProperty>, String> param) {
                return param.getValue().get("age");
            }
        });

        TableColumn<Map<String, SimpleStringProperty>, String> flag = new TableColumn<>("是否合格");
        flag.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Map<String, SimpleStringProperty>, String>, ObservableValue<String>>() {
            @Override
            public ObservableValue<String> call(TableColumn.CellDataFeatures<Map<String, SimpleStringProperty>, String> param) {
                return param.getValue().get("flag");
            }
        });


        tableView.getColumns().addAll(name, age, flag);

        Button button = new Button("button");

        AnchorPane.setTopAnchor(tableView, 50d);
        AnchorPane.setLeftAnchor(tableView, 50d);
        anchorPane.getChildren().addAll(button, tableView);

        Scene scene = new Scene(anchorPane);
        primaryStage.setScene(scene);
        primaryStage.setWidth(800d);
        primaryStage.setHeight(800d);
        primaryStage.setTitle("TableView");
        primaryStage.setResizable(false);
        primaryStage.show();
    }
}
